package com.store.practice.controller;

import com.store.practice.common.api.CommonPage;
import com.store.practice.common.api.CommonResult;
import com.store.practice.model.BgMember;
import com.store.practice.model.BgStore;
import com.store.practice.model.BgStoreStaff;
import com.store.practice.service.BgMemberService;
import com.store.practice.service.BgOrderCaseService;
import com.store.practice.service.BgStoreService;
import com.store.practice.service.BgStoreStaffService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/bgStore")
@Api(tags = "BgStoreController",description = "门店管理")
public class BgStoreController {
    @Autowired
    private BgStoreService storeService;
    @Autowired
    private BgMemberService memberService;
    @Autowired
    private BgStoreStaffService staffService;
    @Autowired
    private BgOrderCaseService orderCaseService;

    @ApiOperation("新增门店信息")
    @PostMapping("/insert")
    public CommonResult insert(@RequestBody BgStore store){
        int i = storeService.insert(store);
        if (i>0){
            return CommonResult.success(i);
        }
        return CommonResult.failed();
    }

    @ApiOperation("编辑门店信息")
    @PostMapping("/update")
    public CommonResult update(@RequestBody BgStore store){
        int i = storeService.update(store);
        if (i>0){
            return CommonResult.success(i);
        }
        return CommonResult.failed();
    }

    @ApiOperation("展示门店列表信息")
    @PostMapping("/list")
    public CommonResult<CommonPage<Map>> list(){
        List<BgStore> list = storeService.list();
        Map<String, Object> storeMap = new HashMap<>();
        List<Map> result = new ArrayList<>();
        if (list!=null){
            for (BgStore store:list){
                storeMap.put("storeId",store.getId());
                storeMap.put("storeName",store.getName());
                storeMap.put("storeManager",store.getManager());
                storeMap.put("contactNum",store.getContactNum());
                result.add(storeMap);
            }
        }
        return CommonResult.success(CommonPage.restPage(result));
    }

    @ApiOperation("展示会员列表")
    @GetMapping("/listMember")
    public CommonResult<CommonPage<BgMember>> listMember(){
        List<BgMember> list = memberService.list();
        if (list == null){
            return CommonResult.failed();
        }
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("展示门店员工列表")
    @GetMapping("/listStoreStaff")
    public CommonResult<CommonPage<Map>> listStaff(){
        List<Map> list = staffService.list();
        if (list==null){
            return CommonResult.failed();
        }
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("查看门店员工详情")
    @GetMapping("/getStoreStaff")
    public CommonResult getStoreStaff(Integer id){
        Map result = staffService.getStaffById(id);
        return CommonResult.success(result);
    }

    @ApiOperation("展示门店预约情况列表")
    @GetMapping("/listStoreOrder")
    public CommonResult listStoreOrder(Integer storeId, @DateTimeFormat(pattern = "yyyy-MM-dd") Date date){
        List<Map> result = orderCaseService.listStoreOrder(storeId, date);
        return CommonResult.success(CommonPage.restPage(result));
    }

    @ApiOperation("展示会员预约情况列表")
    @GetMapping("/listMemberOrder")
    public CommonResult listMemberOrder(Integer mid,@DateTimeFormat(pattern = "yyyy-MM-dd") Date date){
        List<Map> result = orderCaseService.listMemBerOrder(mid, date);
        return CommonResult.success(CommonPage.restPage(result));
    }
}
